package com.jtzc.aikf.service;

import com.jtzc.aikf.entity.DocumentDO;
import com.jtzc.aikf.file.DocumentLoadFactory;
import com.jtzc.aikf.file.DocumentSplitterFactory;
import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.DocumentSplitter;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.EmbeddingStoreIngestor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @author wu chuang
 * @description
 */
@Service
public class VectorService {

    @Autowired
    DocumentLoadFactory documentLoadFactory;

    @Autowired
    DocumentSplitterFactory documentSplitterFactory;

    @Autowired
    private EmbeddingStore embeddingStore;

    @Autowired
    private EmbeddingModel embeddingModel;


    public void learn(DocumentDO documentDO){
        Document document = documentLoadFactory.getDocumentLoader(documentDO.getFileType()).loadDocument(documentDO.getFilePath());
        DocumentSplitter paragraphSplitter = documentSplitterFactory.getDocumentSplitter("ParagraphSplitter");
        List<TextSegment> split = paragraphSplitter.split(document);
        List<Document> documents = Arrays.asList(document);

        //文本向量化并存入向量数据库：将每个片段进行向量化，得到一个嵌入向量
        EmbeddingStoreIngestor
                .builder()
                .embeddingStore(embeddingStore)
                .embeddingModel(embeddingModel)
                .build()
                .ingest(documents);
    }
}
